home *** CD-ROM | disk | FTP | other *** search
- Path: s02.pavilion.co.uk!usenet
- From: AJRobb@pavilion.co.uk (Andy J Robb)
- Newsgroups: comp.lang.c
- Subject: Re: passing arrays and returning structs
- Date: Tue, 12 Mar 1996 23:47:05 GMT
- Organization: Pavilion Internet plc
- Message-ID: <4i52bm$5el@s02.pavilion.co.uk>
- References: <4i2128$69d@news2.acs.oakland.edu>
- NNTP-Posting-Host: poolc27.pavilion.co.uk
- X-Newsreader: Forte Free Agent 1.0.82
-
- jggoslin@vela.acs.oakland.edu (Monument) wrote:
-
- >I have been wracking my brains over this one. I needed to write a
- >program to do the transitive closure of a binary relation matrix, no
- >problems encountered during that. However, I tried to streamline the
- >code, by putting the transitive closure algorithm in it's own
- >function. When I did this, I tried to pass the initially read in
- >matrix into the function as an argument, and return a message buffer
- >to the calling function.
-
- >I got a mess of errors, included below for your perusal. If anyone
- >has any suggestions on how I could get the pointers and returns fixed,
- >I would sincerely appreciate it. Post or email, since I read both
- >regularly.
-
- >===client.h===
- >#include <stdio.h>
- >#include <sys/errno.h>
- >#include <sys/ipc.h>
- >#include <sys/msg.h>
- >#include <sys/types.h>
-
- >#define PATH "./server"
- >#define PROJ 'B'
- >#define PERMS 0666
- >#define MAXSZ 200
-
- >key_t key;
- >int msgqid;
-
- >typedef struct my_msgbuf {
- > long mtype;
- > int pid;
- > int size;
- > int data[2*MAXSZ];
- >} Message;
- >===client.h===
-
- >===client.c===
- >// header files
- >#include "client.h"
-
- >// function prototypes
-
- Message is NOT a struct - you typedef'd it.
-
- >struct Message transitive_closure(int matrix[]);
-
- Message transitive_closure(int matrix[]);
-
- >// MAINLINE
- >int main(int argc, char *argv[])
- >{
- >[I am going to delete a bunch of code here that is irrelevant to the
- >problem, since I know it works. All the code I delete does is reads
- >in the matrix]
-
- >[here is the relevant call]
- > // get the transitive closure
- > buffer = transitive_closure(matrix);
-
- > // print out results
- > puts("Transitive Closure:");
- > for (i=0; i<buffer.size; i++)
- > {
- > for (j=0; j<buffer.size; j++)
- > printf(" %d", buffer.data[i*buffer.size+j]);
- > printf("\n");
- > }
-
- > // successful finish
- > return 0;
- >}
-
- The following definition is different from the above declaration.
- It uses a pointer rather than the struct. Also, Message is still not
- a struct.
-
- >struct Message* transitive_closure(int matrix[])
-
-
- Message transitive_closure(int matrix[])
- >{
- > int i, j, size;
- > Message buffer;
- > pid_t pid;
-
- >[here do I refer to the subtypes in the correct manner? "." vs. "->"
- >is what I'm talking about]
-
- > buffer.mtype=1L;
- > buffer.pid=pid;
- > buffer.size=size;
- > for (i=0; i<size; i++)
- > for (j=0; j<size; j++)
- > buffer.data[i*size+j]=matrix[i*size+j];
-
- >[is the return type correct?]
- No, to be consistent with your redefinition:
- return &buffer; /* bad news guy, it is on the stack!! */
- > return *buffer;
-
- Simply:
- return buffer;
- >}
- >===client.c===
-
- >===ERRORS===
- >cc -g -c client.c
- >/usr/lib/cmplrs/cc/cfe: Error: client.c, line 58: Functions cannot return a non-object type
- > buffer = transitive_closure(matrix);
- > ---------------------------^
- >/usr/lib/cmplrs/cc/cfe: Error: client.c, line 58: Reference an expression of void type or an incomplete type.
- > buffer = transitive_closure(matrix);
- > ---------------------------^
- >/usr/lib/cmplrs/cc/cfe: Error: client.c, line 73: redeclaration of 'transitive_closure'; previous declaration at line 11 in file 'client.c'
- > struct Message* transitive_closure(int matrix[])
- > ----------------^
- >/usr/lib/cmplrs/cc/cfe: Error: client.c, line 73: Incompatible function return type for this function
- > struct Message* transitive_closure(int matrix[])
- > ----------------------------------^
- >/usr/lib/cmplrs/cc/cfe: Error: client.c, line 109: Dereference a non-pointer
- > return *buffer;
- > --------^
- >*** Exit 1
- >Stop.
- >===ERRORS===
-
- > ----------------------------------------------------------------------------
- >| Jeff Goslin - Monument | "Oh Bentson, you are so |
- >| jggoslin@vela.acs.oakland.edu | mercifully free from the |
- >| | ravages of intellect." |
- >| http://www.acs.oakland.edu/links/jggoslin | --Evil, The Time Bandits |
- > ----------------------------------------------------------------------------
- >| how come everyone elses religion is a cult but your cult is a religion |
- > ----------------------------------------------------------------------------
-
- -----BEGIN PGP PUBLIC KEY BLOCK-----
- Version: 2.6.2i
-
- mQCNAy/MpRwAAAEEAOt6uBYqT8yv9EmqNhK8m6v+bYi8QjnGW3Bo6iU1gsMj5pa6
- MHgq99c8deADbE3cbJ6uZS9v5pZE3WCf6HCQjlB5iULA5RZzMdAumd/WUzuL9UT3
- B44D9EqqFIL79FlYb56v4oKFqFp1/J2bIpYUwnUvabGzGjdLrpPl4P16x9sNAAUR
- tCNBbmR5IEogUm9iYiA8QUpSb2JiQHBhdmlsaW9uLmNvLnVrPrQhQW5keSBSb2Ji
- IDxBSlJvYmJAcGF2aWxpb24uY28udWs+
- =/wVD
- -----END PGP PUBLIC KEY BLOCK-----
-
-